from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import re, pandas as pd
def coordinate(site):
    # 创建浏览器驱动对象
    driver = webdriver.Firefox()
    driver.get('http://api.map.baidu.com/lbsapi/getpoint/index.html')
    # 显式等待，设置timeout
    wait = WebDriverWait(driver, 9)
    # 判断输入框是否加载
    input = wait.until(
        EC.presence_of_element_located(
            (By.CSS_SELECTOR, '#localvalue')))
    # 判断搜索按钮是否加载
    submit = wait.until(
        EC.element_to_be_clickable(
            (By.CSS_SELECTOR, '#localsearch')))
    # 输入搜索词，点击搜索按钮
    input.clear()
    input.send_keys(site)
    submit.click()
    # 等待坐标
    wait.until(
        EC.presence_of_element_located(
            (By.CSS_SELECTOR, '#no_0')))
    # 获取网页文本，提取经纬度
    source = driver.page_source
    xy = re.findall('坐标：([\d.]+),([\d.]+)', source)
    # 转浮点数，取中位数
    df = pd.DataFrame(xy, columns=['longitude', 'latitude'])
    df['longitude'] = pd.to_numeric(df['longitude'])
    df['latitude'] = pd.to_numeric(df['latitude'])
    longitude = df['longitude'].median()
    latitude = df['latitude'].median()
    # 关闭浏览器驱动
    driver.close()
    return [longitude, latitude]
print(coordinate('公益西桥站'))